﻿using Abp.Collections.Extensions;
using Abp.Dependency;
using Abp.Extensions;
using Mt.Site.Core.WorkFlows.Design.Cache;
using Mt.Site.Core.WorkFlows.Enum;
using Mt.Site.Core.WorkFlows.Process;
using Mt.Site.Core.WorkFlows.Process.Models;
using Mt.Site.Core.WorkFlows.Process.Stores;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Mt.Site.Core.WorkFlows.Diy.Plugin.Impl.tougao
{
    public class TougaoDengjiNode : WorkFlowNodePluginFunctionsBase, ITransientDependency
    {
        public const string Name = "WFNode.tougao.Dengji";

        public override string DisplayName => "新闻投稿（拟稿）";

        public override string UniqeName => Name;

        private readonly IWorkFlowFormDataStore _workFlowFormDataStore;

        public TougaoDengjiNode(IWorkFlowFormDataStore workFlowFormDataStore)
        {
            _workFlowFormDataStore = workFlowFormDataStore;
        }


        /// <inheritdoc/>
        public override async Task AfterSaveFormAsync(UpdateFormDataInput input, IWorkFlowManager workFlowManager)
        {
            var paper = workFlowManager.CurrentPaper.Paper;
            if (input.Values.ContainsKey(WorkFlowConst.FieldName.Content2.Title))
            {
                paper.ChangeTitle(input.Values[WorkFlowConst.FieldName.Content2.Title]);
            }

            if (input.Values.ContainsKey(WorkFlowConst.FieldName.Content2.PublishTime))
            {
                paper.ChangeRegistrationTime(input.Values[WorkFlowConst.FieldName.Content2.PublishTime]);
            }
            else
            {
                paper.ChangeRegistrationTime(null);
            }

            await Task.FromResult(0);
        }

        /// <inheritdoc/>
        public async override Task<StateWhenSubmit> CheckDataBeforeSelelctUsersToNextNode(IWorkFlowManager workFlowManager, bool isNeedToSelectUsers)
        {
            await DiyStaticFunction.TougaoFormDataCheckAsync(_workFlowFormDataStore, workFlowManager.PaperId);

            var nextIdList = await workFlowManager.GetNextAsync();
            if (!nextIdList.IsNullOrEmpty())
            {
                var nextNode = nextIdList[0];

                //调取人员选择框的选项及默认选项
                var userIds = await workFlowManager
                    .GetUserListAsync(nextNode.Definition.NodeId, null, nextNode.Definition.ToUserFilter());
                var userIdList = userIds.DefaultSelectedUsers.Select(c => c.Id).ToList();
                if (userIdList.Count > 0)
                {
                    //不再弹 选人对话框
                    return new StateWhenSubmit
                    {
                        IsNeedToSelectUsers = false
                    };
                }
            }

            return new StateWhenSubmit
            {
                IsNeedToSelectUsers = true
            };
        }

        /// <inheritdoc/>
        public override async Task<bool> DiyGoNextAsync(
            IWorkFlowManager workFlowManager, Guid nodeId, List<long> userids_next, List<long> turnRoundUserList, ActionScheme actionScheme_next, string replyMsg)
        {
            return await GoNextAutoAsync(workFlowManager, replyMsg);
        }
    }
}
